package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * 短信策略   控制   监听器Listener
 * @author lixu
 */
@Component
public class PreSendQueueListener {

    /**
     * 责任链的管理者
     */
    @Autowired
    private FilterChainManager filterChainManager;

    /**
     * 监听 接口模块微服务 发送过来的客户提交的表单对象
     *
     *     // 发送短信的消息队列名  ---  接口模块发送消息到策略模块
     *     public final static String TOPIC_PRE_SEND = "pre_send_sms_topic";
     */
    //@RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)

    /**
     * 使用注解创建队列  创建交换机 绑定RoutingKey  再监听队列)
     * @param message
     * @param channel
     * @param standardSubmit
     * @throws Exception
     *
     *

    pre_send_sms_topic
     *
     *   //推送状态报告TOPIC    ---      让接口模块给客户端一个推送.
     *     public final static String TOPIC_PRE_SEND = "pre_send_sms_topic";
     *     //交换机名称
     *     RabbitMQConstants.TOPIC_PRE_SEND + "_exchange"
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = RabbitMQConstants.TOPIC_PRE_SEND,durable = "true",autoDelete = "false"),
                    exchange = @Exchange(value = RabbitMQConstants.TOPIC_PRE_SEND + "_exchange"),
                    key = {"#"}
            )
    })
    public void consume( Message message, Channel channel, StandardSubmit standardSubmit)  throws Exception{
        //StandardSubmit  : 客户端  发送过的一大表单
        filterChainManager.executeFilter(standardSubmit) ;//执行大量的策略链
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
